#include "Debug.h"
#include <iostream>
#include <fstream>

Poco::Mutex* Debug::mutex = new Poco::Mutex;	// Get a new mutex
/*********************************************************/
Debug& Debug::Instance( ) 
{
	mutex->lock();
	static Debug pInstance;
	mutex->unlock();
	return pInstance; 			// return the address of the sole instance
}
/*********************************************************/
Debug::~Debug()
{
	delete( mutex );
}
/*********************************************************/
std::string Debug::flush()
{
	return( debug.str() );
}
/*********************************************************/
void Debug::flush(std::ostream& ostr)
{
	mutex->lock();
	ostr << "Debug:>" << std::endl;
	ostr << debug.str();
	mutex->unlock();
}
/*********************************************************/
std::ostream& Debug::operator<<(const std::string& msg)
{
	mutex->lock();
	if ( debug.good() ) {
		debug << msg;
	}
	mutex->unlock();
	return( debug );
}
/*********************************************************/
void Debug::log(const std::string& name)
{
	std::ofstream file;
	file.open(name.c_str());
	if ( file.is_open() ) {
		file << debug.str();
	} else {
		std::string message = "Could not open file: ";
		message.append(name);
		throw(std::logic_error(message.c_str()));
	}
	file.close();
}
